package my.hnb.android.view.control;

import my.hnb.android.listenner.LoadingControlListener;
import my.hnb.songnhac.R;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class LoadingControl extends RelativeLayout {
	public static final int STATE_AVAIABLE = 0;
	public static final int STATE_UN_AVAIABLE = 1;
	public static final int STATE_LOADING = 2;
	public static final int STATE_NOT_LOADING = 3;

	private ProgressBar pbLoading;
	private TextView tvLoading;
	private int state;
	private LoadingControlListener listener;

	public LoadingControl(Context context, LoadingControlListener lis) {
		super(context);
		// TODO Auto-generated constructor stub
		listener = lis;
		LayoutInflater inflater = (LayoutInflater) getContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(R.layout.loading_control, this);
		pbLoading = (ProgressBar) v.findViewById(R.id.pb_loading);
		tvLoading = (TextView) v.findViewById(R.id.tv_loading);
		pbLoading.setVisibility(View.GONE);
		setLoadingState(STATE_UN_AVAIABLE);
	}
	
	public LoadingControl(Context context){
		super(context);
		innit();
	}

	
	public LoadingControl(Context context, AttributeSet att){
		super(context);
		innit();
	}
	
	
	private void innit(){
		LayoutInflater inflater = (LayoutInflater) getContext()
			.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(R.layout.loading_control, this);
		pbLoading = (ProgressBar) v.findViewById(R.id.pb_loading);
		tvLoading = (TextView) v.findViewById(R.id.tv_loading);
		pbLoading.setVisibility(View.GONE);
		setLoadingState(STATE_LOADING);
	}
	
	
	/**
	*  return ve trang thai hien tai cua loading control
	*  @author: BangHN
	*  @return: int - state
	*  @throws:
	 */
	public int getLoadingState(){
		return this.state;
	}
	
	public void setLoadingState(int state) {
		this.state = state;
		switch (state) {
		case STATE_AVAIABLE:
			pbLoading.setVisibility(View.GONE);
			break;
		case STATE_UN_AVAIABLE:
			pbLoading.setVisibility(View.GONE);
			break;
		case STATE_LOADING:
			tvLoading.setText(R.string.song_loading);
			pbLoading.setVisibility(View.VISIBLE);
			break;
		case STATE_NOT_LOADING:
			pbLoading.setVisibility(View.GONE);
			tvLoading.setText(R.string.error_loading);
			break;

		default:
			break;
		}
	}

	public void handleLoading() {
		switch (state) {
		case STATE_AVAIABLE:
			if (listener != null) {
				setLoadingState(STATE_LOADING);
				listener.load();
			}
			break;
		case STATE_UN_AVAIABLE:

			break;
		case STATE_LOADING:

			break;
		case STATE_NOT_LOADING:

			break;

		default:
			break;
		}
	}
}
